package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper planMapper;



    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> all() {
        List<NursingPlanVo> nursingProjectpPlanVos = planMapper.all();
        return nursingProjectpPlanVos;
    }

    /**
     * 分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> list(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingPlanVo> page = planMapper.list(name,status);
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>();
        pageResponse.setPageSize(pageSize);
        pageResponse.setPage(pageNum);
        pageResponse.setTotal(page.getTotal());
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理计划
     * @param planDto
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void add(NursingPlanDto planDto) {
            NursingPlan plan = BeanUtil.toBean(planDto, NursingPlan.class);
            plan.setStatus(1);
            planMapper.add(plan);
            planDto.getProjectPlans().forEach(v -> v.setPlanId(Math.toIntExact(plan.getId())));
            planMapper.addList(planDto.getProjectPlans());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public NursingPlanVo findByid(Long id) {
        NursingPlanVo nursingPlanVo = planMapper.findByid(id);
        List<NursingProjectPlan> lists = planMapper.Byid(id);
        nursingPlanVo.setProjectPlans(lists);
        return nursingPlanVo;
    }

    /**
     * 根据id修改
     * @param nursingPlanDto
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updata(NursingPlanDto nursingPlanDto) {
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,plan);
        planMapper.updata(plan);
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();

        planMapper.baseDelete(plan.getId());

        planMapper.upDataId(projectPlans);

    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void delete(Long id) {

        planMapper.delete(id);
        planMapper.deleteId(id);
    }

    /**
     * 禁用启用
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        planMapper.updateStatus(id, status);
    }
}
